package org.dromara.pe.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.DataAcquisitionBo;
import org.dromara.pe.domain.vo.DataAcquisitionVo;
import org.dromara.pe.domain.DataAcquisition;
import org.dromara.pe.mapper.DataAcquisitionMapper;
import org.dromara.pe.service.IDataAcquisitionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据采集记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class DataAcquisitionServiceImpl implements IDataAcquisitionService {

    private final DataAcquisitionMapper baseMapper;

    /**
     * 查询数据采集记录
     */
    @Override
    public DataAcquisitionVo queryById(Long acquisitionId){
        return baseMapper.selectVoById(acquisitionId);
    }

    /**
     * 查询数据采集记录列表
     */
    @Override
    public TableDataInfo<DataAcquisitionVo> queryPageList(DataAcquisitionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DataAcquisition> lqw = buildQueryWrapper(bo);
        Page<DataAcquisitionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据采集记录列表
     */
    @Override
    public List<DataAcquisitionVo> queryList(DataAcquisitionBo bo) {
        LambdaQueryWrapper<DataAcquisition> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DataAcquisition> buildQueryWrapper(DataAcquisitionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DataAcquisition> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNum()), DataAcquisition::getStuNum, bo.getStuNum());
        lqw.eq(StringUtils.isNotBlank(bo.getAchievement()), DataAcquisition::getAchievement, bo.getAchievement());
        lqw.eq(StringUtils.isNotBlank(bo.getSources()), DataAcquisition::getSources, bo.getSources());
        lqw.eq(StringUtils.isNotBlank(bo.getSourcesType()), DataAcquisition::getSourcesType, bo.getSourcesType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DataAcquisition::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增数据采集记录
     */
    @Override
    public Boolean insertByBo(DataAcquisitionBo bo) {
        DataAcquisition add = MapstructUtils.convert(bo, DataAcquisition.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAcquisitionId(add.getAcquisitionId());
        }
        return flag;
    }

    /**
     * 修改数据采集记录
     */
    @Override
    public Boolean updateByBo(DataAcquisitionBo bo) {
        DataAcquisition update = MapstructUtils.convert(bo, DataAcquisition.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DataAcquisition entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据采集记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
